package org.dfl.plugin;

import org.dfl.messages.Messages;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/**
 * preference page to enter graphviz location
 * @author Piotr Wlodarczyk
 *
 */
public class DFLPluginPreferencePage extends PreferencePage implements
		IWorkbenchPreferencePage {

	private Text dotExeLocationField;

	@Override
	protected Control createContents(Composite parent) {
		Composite entry = new Composite(parent, SWT.NULL);
		
		RowLayout thisLayout = new RowLayout(org.eclipse.swt.SWT.HORIZONTAL);
		entry.setLayout(thisLayout);
		{
			Label label1 = new Label(entry, SWT.NONE);
			label1.setText(Messages.DFLPluginPreferencePage_0);
		}
		{
			dotExeLocationField = new Text(entry, SWT.NONE);
			RowData dotExeLocationField1LData = new RowData();
			dotExeLocationField1LData.width = 200;
			dotExeLocationField1LData.height = 23;
			dotExeLocationField.setLayoutData(dotExeLocationField1LData);
			dotExeLocationField.setText(DflPlugin.getDefault().getDotLocationPreference());
		}
		{
			Button dotExeLocationSelectButton = new Button(entry, SWT.PUSH | SWT.CENTER);
			RowData button1LData = new RowData();
			button1LData.width = 71;
			button1LData.height = 23;
			dotExeLocationSelectButton.setLayoutData(button1LData);
			dotExeLocationSelectButton.setText(Messages.DFLPluginPreferencePage_1);
			
			dotExeLocationSelectButton.addListener(SWT.Selection,new Listener(){
				public void handleEvent(Event arg0) {
					FileDialog fd = new FileDialog(new Shell(Display.getDefault()), SWT.OPEN);
			        fd.setText(Messages.DFLPluginPreferencePage_2);
			        fd.setFilterPath("C:/"); //$NON-NLS-1$
			        String[] filterExt = { "*.exe"}; //$NON-NLS-1$
			        fd.setFilterExtensions(filterExt);
			        String selected = fd.open();
			        
					if(selected!=null) dotExeLocationField.setText(selected);
				}
			});
		}
		
		return entry;
	}

	public void init(IWorkbench workbench) {
		//Initialize the preference store we wish to use
		setPreferenceStore(DflPlugin.getDefault().getPreferenceStore());
	}
	/**
	 * Performs special processing when this page's Restore Defaults button has been pressed.
	 * Sets the contents of the nameEntry field to
	 * be the default 
	 */
	protected void performDefaults() {
		dotExeLocationField.setText(""); //$NON-NLS-1$
	}
	
	/** 
	 * Method declared on IPreferencePage. Save the
	 * author name to the preference store.
	 */
	public boolean performOk() {
		DflPlugin.getDefault().setDotLocationPreference(dotExeLocationField.getText());
		return super.performOk();
	}

}
